/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.cspimp.mhm.service.impl;

import me.zhengjie.modules.cspimp.mhm.domain.DeviceSettingManage;
import me.zhengjie.modules.cspimp.psp.domain.Accident;
import me.zhengjie.modules.cspimp.psp.service.dto.AccidentDto;
import me.zhengjie.modules.system.domain.AreaManage;
import me.zhengjie.modules.system.repository.AreaManageRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.mhm.repository.DeviceSettingManageRepository;
import me.zhengjie.modules.cspimp.mhm.service.DeviceSettingManageService;
import me.zhengjie.modules.cspimp.mhm.service.dto.DeviceSettingManageDto;
import me.zhengjie.modules.cspimp.mhm.service.dto.DeviceSettingManageQueryCriteria;
import me.zhengjie.modules.cspimp.mhm.service.mapstruct.DeviceSettingManageMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author 黄星星
 * @website https://docs.auauz.net
 * @description 服务实现
 * @date 2020-05-30
 **/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeviceSettingManageServiceImpl implements DeviceSettingManageService {

    private final DeviceSettingManageRepository deviceSettingManageRepository;
    private final DeviceSettingManageMapper deviceSettingManageMapper;
    private final AreaManageRepository areaManageRepository;

    @Override
    public Map<String, Object> queryAll(DeviceSettingManageQueryCriteria criteria, Pageable pageable) {
        List<DeviceSettingManage> page = deviceSettingManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        List<DeviceSettingManageDto> deviceSettingManageList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page)) {
            for (DeviceSettingManage deviceSettingManage : page) {
                DeviceSettingManageDto deviceSettingManageDto = new DeviceSettingManageDto();
                BeanUtils.copyProperties(deviceSettingManage, deviceSettingManageDto);
                AreaManage areaManage = areaManageRepository.findById(deviceSettingManageDto.getAreaId()).orElse(null);
                if (null != areaManage) {
                    deviceSettingManageDto.setAreaName(areaManage.getAreaName());
                }
                deviceSettingManageList.add(deviceSettingManageDto);
            }
        }

        return PageUtil.toPage(PageUtil.listPage(deviceSettingManageList, pageable));
    }

    @Override
    public List<DeviceSettingManageDto> queryAll(DeviceSettingManageQueryCriteria criteria) {
        return deviceSettingManageMapper.toDto(deviceSettingManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public DeviceSettingManageDto findById(Long id) {
        DeviceSettingManage deviceSettingManage = deviceSettingManageRepository.findById(id).orElseGet(DeviceSettingManage::new);
        ValidationUtil.isNull(deviceSettingManage.getId(), "DeviceSettingManage", "id", id);
        return deviceSettingManageMapper.toDto(deviceSettingManage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DeviceSettingManageDto create(DeviceSettingManage resources) {
        return deviceSettingManageMapper.toDto(deviceSettingManageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DeviceSettingManage resources) {
        DeviceSettingManage deviceSettingManage = deviceSettingManageRepository.findById(resources.getId()).orElseGet(DeviceSettingManage::new);
        ValidationUtil.isNull(deviceSettingManage.getId(), "DeviceSettingManage", "id", resources.getId());
        deviceSettingManage.copy(resources);
        deviceSettingManageRepository.save(deviceSettingManage);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            deviceSettingManageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DeviceSettingManageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DeviceSettingManageDto deviceSettingManage : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("创建时间", deviceSettingManage.getCreateTime());
            map.put("更新时间", deviceSettingManage.getUpdateTime());
            map.put("创建者", deviceSettingManage.getCreateBy());
            map.put("更新者", deviceSettingManage.getUpdateBy());
            map.put("设备名称", deviceSettingManage.getDeviceName());
            map.put("设备编号", deviceSettingManage.getDeviceCode());
            map.put("设备类型", deviceSettingManage.getDeviceType());
            map.put("所在区域", deviceSettingManage.getAreaId());
            map.put("模型坐标", deviceSettingManage.getModelCoordinates());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}